﻿Public MustInherit Class BaseUserControl
    Inherits System.Web.UI.UserControl
    Implements IBaseUserControl

    Public MustOverride Sub Rebind() Implements IBaseUserControl.Rebind
    Public Event OnUpdate(ByVal sender As Object, ByVal e As System.EventArgs) Implements IBaseUserControl.OnUpdate
    Protected PromptControl As New LiteralControl

    Public Sub NotifyUpdate(ByVal sender As Object, ByVal e As System.EventArgs) Implements IBaseUserControl.NotifyUpdate
        Me.Page.NeedUpdate(sender, e)
    End Sub

    Public Shadows ReadOnly Property Page() As BasePage Implements IBaseUserControl.Page
        Get
            Return CType(MyBase.Page, BasePage)
        End Get
    End Property

    Public Sub Update(ByVal sender As Object, ByVal e As System.EventArgs) Implements IBaseUserControl.Update
        RaiseEvent OnUpdate(sender, e)
    End Sub

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Controls.Add(PromptControl)
    End Sub

    Public Sub RaiseError(ByVal ex As System.Exception) Implements IBaseUserControl.RaiseError
        For Each control As Control In Me.Controls
            control.Visible = False
        Next
        Prompt("An unexpected error happened during the operation!", UserControlPromptTypes.Error)
        'Prompt(ex.ToString, UserControlPromptTypes.Error)
    End Sub

    Public Function GetParentBaseUserControl(ByVal control As System.Web.UI.Control) As IBaseUserControl Implements IBaseUserControl.GetParentBaseUserControl
        Dim tempControl As Control = control
        If tempControl.NamingContainer Is Me OrElse tempControl Is Me Then
            Return Me
        Else
            While tempControl IsNot Nothing
                If TypeOf tempControl.NamingContainer Is BaseUserControl Then
                    Return tempControl.NamingContainer
                Else
                    tempControl = tempControl.NamingContainer
                End If
            End While
        End If
        Return Nothing
    End Function

    Public Sub Prompt(ByVal message As String, ByVal promptType As UserControlPromptTypes) Implements IBaseUserControl.Prompt
        Dim imageUrl As String = "/Images/"
        Dim cssClass As String = ""
        Select Case promptType
            Case UserControlPromptTypes.Alert
                imageUrl += "alert.gif"
                cssClass = "alertPrompt"
            Case UserControlPromptTypes.Error
                imageUrl += "error.gif"
                cssClass = "errorPrompt"
            Case UserControlPromptTypes.Successful
                imageUrl += "success.gif"
                cssClass = "successPrompt"
        End Select
        PromptControl.Text = "<div class=""" & cssClass & """><img src=""" & imageUrl & """ />" & message & "</div>"
        PromptControl.Visible = True
    End Sub

    Private Sub Page_OnUpdate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.OnUpdate
        If TypeOf sender Is Control Then
            If GetParentBaseUserControl(sender) IsNot Me Then
                PromptControl.Text = ""
                PromptControl.Visible = False
                Rebind()
            End If
        End If
    End Sub

End Class
